package org.java.web;

import org.java.dao.DaoImpl.ArtistsDao;
import org.java.dao.DaoImpl.GoodsDao;
import org.java.entity.Artists;
import org.java.entity.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/init")
public class InitServlet extends BaseServlet {
    public GoodsDao dao = new GoodsDao();
    private ArtistsDao dao1 = new ArtistsDao();
    protected void mainInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //默认当前页为第1页
        int page=1;

        //获得page的请求,如果有值,就将默认page覆盖
        String curenPage = request.getParameter("page");
        if (curenPage!=null){
            page = Integer.parseInt(curenPage);//不为空,覆盖默认值
        }

        //默认每一页显示6条数据
        int rows=6;

        //查询数据表得到数据总数
        int count = dao.getCount();

        //计算总页数
        int maxPage = count % rows==0? count/rows: count/rows+1;

        //查询,得到集合
        List<Goods> list=dao.getList(page,rows);

        List<Artists> list2 = dao1.getList();

        //将数据存放在请求中
        request.setAttribute("list",list);

        request.setAttribute("list2",list2);

        //存储分页相关数据
        request.setAttribute("page",page);
        request.setAttribute("rows",rows);
        request.setAttribute("count",count);
        request.setAttribute("maxPage",maxPage);

        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
